use crate::controller::Context;
use crate::model::state_code::{StateCode, StateCodeError};
use string_util;
use warp::Rejection;
use crate::{notify, rspMap, protos, receiveProtoHandler, PROTO_SENDER};
use crate::ProtoId::ProtoID;
use num_traits::FromPrimitive;
use crate::protos::qot_common::Security;
use crate::receiveProtoHandler::ReceiveProtoErr;
use prost::DecodeError;
use crate::common::request::request_to_srv;
use crate::protos::qot_reg_qot_push::C2s;


pub async fn register(ctx: Context) -> Result<impl warp::Reply, Rejection> {
    ctx.response_code(StateCode::Success)
}

pub async fn login(ctx: Context) -> Result<impl warp::Reply, Rejection> {
    // 参数检验
    let name: String = ctx.param("name")?;
    if name.is_empty() || name.len() > 125 || string_util::is_have_special_char(name.as_str()) {
        return ctx.response_code(StateCode::ParamError);
    }
    let password: String = ctx.param("password")?;
    if password.is_empty()
        || password.len() > 125
        || string_util::is_have_special_char(password.as_str())
    {
        return ctx.response_code(StateCode::ParamError);
    }

    ctx.response_code(StateCodeError::from_code_str(
        StateCode::Success,
        format!("hello {}", &name),
    ))
}

pub async fn get_kl(ctx: Context) -> Result<impl warp::Reply, Rejection> {
    let request = protos::qot_get_kl::Request {
        c2s: protos::qot_get_kl::C2s {
            rehab_type: 2,
            kl_type: 1,
            security: Security { market: 1, code: "your范德萨外网".to_string() },
            req_num: 1,
        }
    };
    request_to_srv(ctx, ProtoID::QOT_GETKL, &request).await
}
pub async fn qot_reg_qot_push(ctx: Context) -> Result<impl warp::Reply, Rejection> {
    let request = protos::qot_reg_qot_push::Request {
        c2s: protos::qot_reg_qot_push::C2s {
            security_list: vec![],
            sub_type_list: vec![],
            rehab_type_list: vec![],
            is_reg_or_un_reg: false,
            is_first_push: None
        }
    };
    request_to_srv(ctx, ProtoID::QOT_REGQOTPUSH, &request).await
}
pub async fn qot_sub(ctx: Context) -> Result<impl warp::Reply, Rejection> {
    let request = protos::qot_sub::Request {
        c2s: protos::qot_sub::C2s {
            security_list: vec![],
            sub_type_list: vec![],
            is_sub_or_un_sub: false,
            is_reg_or_un_reg_push: None,
            reg_push_rehab_type_list: vec![],
            is_first_push: None,
            is_unsub_all: None,
            is_sub_order_book_detail: None,
            extended_time: None
        }
    };
    request_to_srv(ctx, ProtoID::QOT_SUB, &request).await
}

